package com.qjjbmt.zhq_blog_admin.controller;

import com.qjjbmt.zhq_blog_admin.pojo.Comment;
import com.qjjbmt.zhq_blog_admin.pojo.Reply;
import com.qjjbmt.zhq_blog_admin.pojo.User;
import com.qjjbmt.zhq_blog_admin.service.impl.BlogServiceImpl;
import com.qjjbmt.zhq_blog_admin.service.impl.CommentServiceImpl;
import com.qjjbmt.zhq_blog_admin.service.impl.ReplyServiceImpl;
import com.qjjbmt.zhq_blog_admin.service.impl.UserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;


@Controller
class CommentController {

    UserServiceImpl userService;
    CommentServiceImpl commentService;
    BlogServiceImpl blogService;

    public CommentController(CommentServiceImpl commentService, BlogServiceImpl blogService,
                             UserServiceImpl userService){
        this.commentService = commentService;
        this.blogService = blogService;
        this.userService = userService;
    }

    /*
    查询所有评论
     */
    @RequestMapping("/comments")
    public String list(Model model){
        Collection<Comment> comments = commentService.list();
        model.addAttribute("comments",comments);
        return "comment/comment_list";
    }

    /*
    跳转到添加页面
     */
    @GetMapping("/addComment")
    public String toAddCommentPage(Model model){
        model.addAttribute("users",userService.list());
        model.addAttribute("blogs",blogService.list());
        return "comment/add";
    }

     /*
    添加评论，然后刷新列表
     */
    @PostMapping("/addComment")
    public String addComment(Comment comment){
        User user = userService.getById(comment.getNickName());
        comment.setNickName(user.getUsername());
        comment.setUserLogo(user.getIconUrl());
        comment.setImgId("xcclsscrt0tev11ok364");

        LocalDateTime dateTime = LocalDateTime.now();
        comment.setCreateDate(dateTime);

        commentService.save(comment);
        return "redirect:/comments";
    }

    /*
    根据id查询评论，然后跳转到修改页面并展示
     */
    @GetMapping("/comment/{id}")
    public String toUpdateCommentPage(@PathVariable("id")Integer id,Model model){
        //查出原来的数据
        model.addAttribute("comment",commentService.getById(id));
        model.addAttribute("blogs",blogService.list());
        return "comment/update";
    }

    /*
    保存管理员的修改
     */
    @RequestMapping("/updateComment")
    public String updateComment(Comment comment){
        commentService.updateById(comment);
        return "redirect:/comments";
    }

    /*
    根据id删除评论，然后刷新列表
     */
    @GetMapping("/delComment/{id}")
    public String deleteComment(@PathVariable("id") Integer id){
        commentService.removeById(id);
        return "redirect:/comments";
    }

}
